﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq.Mapping;
using Microsoft.Data.Sqlite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using KorbeRTR.Utility;

namespace KorbeRTR.Models
{
    [Table(Name = "FBGDMapping")]
    public class FBGD
    {
        [Column(Name = "BG")]
        public int 板高 { get; set; }

        [Column(Name = "WZ")]
        public int 位置 { get; set; }

        public static Dictionary<int, FBGD> ListAll()
        {
            var dt = FBGDDBHelper.ExecuteTable("SELECT * FROM FBGDMapping");

            var dict = new Dictionary<int, FBGD>();
            foreach (DataRow dr in dt.Rows)
            {
                var md = dr.ToModel<FBGD>();
                dict[md.板高] = md;
            }

            return dict;
        }

        public static bool Update(FBGD fbgd)
        {
            var rows = FBGDDBHelper.ExecuteNonQuery(
                @"UPDATE FBGDMapping SET WZ=@WZ WHERE BG=@BG",
                new SqliteParameter("@WZ", fbgd.位置),
                new SqliteParameter("@BG", fbgd.板高)
                );
            return rows > 0;
        }

        public static bool Update(SqliteTransaction trans, FBGD fbgd)
        {
            var rows = FBGDDBHelper.ExecuteNonQuery(trans,
                @"UPDATE FBGDMapping SET WZ=@WZ WHERE BG=@BG",
                new SqliteParameter("@WZ", fbgd.位置),
                new SqliteParameter("@BG", fbgd.板高)
                );
            return rows > 0;
        }
    }
}
